package com.github.lzyzsd.jsbridge.example;

import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;

/**
 * <pre>
 * Description: web端获取手机信息
 * Copyright  : Copyright (c) 2022
 * Email      : jusenr@163.com
 * Author     : Jusenr
 * Date       : 2022/1/27 9:51
 * Project    ：JsBridge-1.0.4
 * PackageName：com.github.lzyzsd.jsbridge.example.
 * </pre>
 */
public class PlatformInfo extends BaseBindHandler {

    private static final PlatformInfo info = new PlatformInfo();

    private PlatformInfo() {
    }

    public static PlatformInfo getInstance() {
        return info;
    }

    @Override
    public void bindHandler(BridgeWebView webView) {
        webView.registerHandler("platformInfo", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.i(TAG, "registerHandler handler = platformInfo, data from web = " + data);
                ToastUtils.showShort("JS返回: %s", data);

                final String brand = android.os.Build.BRAND;//手机品牌 HONOR
                final String model = android.os.Build.MODEL;//手机型号 JSN-AL00a
                final String release = android.os.Build.VERSION.RELEASE;//手机操作系统 10
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("platform", "Android");
                jsonObject.addProperty("brand", brand);
                jsonObject.addProperty("model", model);
                jsonObject.addProperty("release", release);
                jsonObject.addProperty("versionName", BuildConfig.VERSION_NAME);
                jsonObject.addProperty("versionCode", BuildConfig.VERSION_CODE);
                final String info = jsonObject.toString();
                function.onCallBack(info);
            }
        });
    }
}
